package com.cameron.serviceedu.controller;


import com.cameron.serviceedu.entity.dto.CourseDetailsDto;
import com.cameron.serviceedu.entity.dto.CourseDto;
import com.cameron.serviceedu.entity.viewobject.CourseCondition;
import com.cameron.serviceedu.entity.viewobject.CourseVo;
import com.cameron.serviceedu.mapper.EduCourseMapper;
import com.cameron.serviceedu.service.EduCourseService;
import com.cameron.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author cameron
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/service/edu-course")
@CrossOrigin
@Slf4j
public class EduCourseController {

    @Resource
    private EduCourseService courseService;

    @Resource
    private EduCourseMapper courseMapper;

    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody CourseDto courseDto) {
        return courseService.saveOrUpdate(courseDto);
    }

    @GetMapping("getCourse/{courseId}")
    public Result getInfo(@PathVariable String courseId) {
        return courseService.getCourse(courseId);
    }

    @GetMapping("courseDetails/{courseId}")
    public Result getCourseDetails(@PathVariable String courseId) {
        CourseDetailsDto details = courseMapper.getCourseDetails(courseId);
        return Result.ok().data("courseDetails", details);
    }

    @PutMapping("/publish/{courseId}")
    public Result publish(@PathVariable String courseId) {
        return courseService.publish(courseId);
    }

    @PostMapping("pageByRules/{currentPage}/{size}")
    public Result page(@RequestBody(required = false) CourseCondition condition, @PathVariable int currentPage, @PathVariable int size) {
        return courseService.pageByConditions(condition, currentPage, size);
    }

//    @AdminAuth
    @DeleteMapping("/{courseId}")
    public Result delById(@PathVariable String courseId) {
        log.info(courseId);
        return courseService.removeCourseById(courseId);
    }

    @GetMapping("/course/detail/{courseId}")
    public Result getCourseDetail(@PathVariable String courseId) {
        return Result.ok().data("detail", courseService.getCourseDetail(courseId));
    }

    @PostMapping("/update/course")
    public Result updateCourse(@RequestBody CourseVo courseVo){
        courseService.updateCourse(courseVo);
        return Result.ok();
    }

}

